/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.composite.camel.cdi;

/**
 *
 * @author khaleddaham
 */
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import se.sll.service.catalogue.composite.camel.processor.CapacityProcessor;
import se.sll.service.catalogue.composite.camel.helper.HSAProcessor;
import se.sll.service.catalogue.composite.camel.processor.HTTPHeader;
import se.sll.service.catalogue.composite.camel.processor.OfferProcessor;
import se.sll.service.catalogue.composite.camel.processor.ProductProcessor;
import se.sll.service.catalogue.common.property.CatalogProperties;
import se.sll.service.catalogue.composite.processor.RIVTAResponseProcessor;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class CamelRoute extends RouteBuilder {
    public static final String FROM_CAPACITY_BY_UNIT_ID_LIST = "direct:start:capacity:byunitids";
    public static final String FROM_CAPACITY_GET_ALL = "direct:start:capacity:getall";
    public static final String FROM_PRODUCT_GET_ALL = "direct:start:listproducts";
    public static final String FROM_OFFER_BY_PRODUCT_CODE = "direct:start:offers:byproductcode";
    public static final String TRANSFORM_OFFER_MODEL_TO_RIVTA = "direct:start:offersToRIVTAFindOffers";
    public static final String FROM_OFFER_BY_HSAID = "direct:start:offers:byhsaid";
    public static final String FROM_OFFER_GET_ALL = "direct:start:offers";
    
    @Override
    public void configure() throws Exception {
        /**
         * Fetch capacities for unit id's Body: List<String>
         */
        from(FROM_CAPACITY_BY_UNIT_ID_LIST).bean(new HTTPHeader(), "mimeType")
                .bean(new CapacityProcessor(), "listToCapacityModel")
                .marshal().json(JsonLibrary.Jackson)
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.CAPACITY_BASE_URL) + "/capacity/byunitids"))
                .to("http4://uri")
                .bean(new CapacityProcessor(), "capacityResponseJSON")
                .bean(new RIVTAResponseProcessor(), "fromCapacityToGetCapacities");

        from(FROM_CAPACITY_GET_ALL).bean(new HTTPHeader(), "mimeType")
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.CAPACITY_BASE_URL) + "/capacity/"))
                .to("http4://uri")
                .bean(new CapacityProcessor(), "capacityResponseJSON")
                .bean(new RIVTAResponseProcessor(), "fromCapacityToGetCapacities");

        /**
         * Route to fetch all products and return GetProductsResponse
         * object. Body: empty
         */
        from(FROM_PRODUCT_GET_ALL)
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL) + "/products/"))
                .to("http4://uri")
                .bean(new ProductProcessor(), "productResponseJSON")
                .bean(new RIVTAResponseProcessor(), "fromProductToGetProducts");

        /**
         * Route to fetch all offers for PullOffers
         */
        from(FROM_OFFER_GET_ALL)
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL) + "/units/"))
                .to("http4://uri")
                .bean(new OfferProcessor(), "offerResponseJSON")
                .bean(new RIVTAResponseProcessor(), "fromOfferToPullOffers");

        from(FROM_OFFER_BY_HSAID)
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .bean(new OfferProcessor(), "listToOfferModel")
                .marshal().json(JsonLibrary.Jackson)
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL) + "/units/byunitids"))
                .to("http4://uri")
                .bean(new OfferProcessor(), "offerResponseJSON")
                .choice()
                    .when().simple("${in.body.size} == 0").to(TRANSFORM_OFFER_MODEL_TO_RIVTA)
                    .otherwise()
                .bean(new ProductProcessor(), "buildProductNameQueryParam")
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL) + "/products/names/"))
                .to("http4://uri")
                .bean(new ProductProcessor(), "productResponseJSON")
                .to(TRANSFORM_OFFER_MODEL_TO_RIVTA);

        from(TRANSFORM_OFFER_MODEL_TO_RIVTA)
                .bean(new RIVTAResponseProcessor(), "fromOfferToFindOffers");

        from(FROM_OFFER_BY_PRODUCT_CODE)
                .setHeader(Exchange.HTTP_URI, simple(CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL) + "/units/byproductname/${header.name}"))
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json; charset=UTF-8"))
                .to("http4://uri")
                .bean(new OfferProcessor(), "offerResponseJSON")
                .bean(new ProductProcessor(), "buildProductNameQueryParam")
                /**
                .filter(header("HTTP_QUERY").isNotNull())
                    .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_BASE_URL) + "/products/names/"))
                    .to("http4://uri")
                    */
                .bean(new ProductProcessor(), "productResponseJSON")
                .bean(new RIVTAResponseProcessor(), "fromOfferToFindOffers");

        // HSA
        /**
         * from("direct:start:hsa:all")
         * .to("restlet:http://api.offentligdata.minavardkontakter.se/orgmaster-hsa/v1/hsaObjects/")
         * .bean(new HSAProcessor(), "hsaListResponseJSON");
         */
        /**
         * FindOffers search by product name
         */
        from("direct:start:product")
                .setHeader(Exchange.HTTP_URI, constant(CatalogProperties.getProperty(CatalogProperties.NAME.OFFERS_BASE_URL) + "/units/byproductname/{name}"))
                .to("http4://uri")
                .bean(new OfferProcessor(), "offerResponseJSON");
        from("direct:start:hsa:one")
                .setHeader(Exchange.HTTP_URI, simple("api.offentligdata.minavardkontakter.se/orgmaster-hsa/v1/hsaObjects/{hsaID}.json"))
                .to("http4://uri")
                .bean(new HSAProcessor(), "hsaResponseJSON");
    }
}
